package com.pickgliss.light2d.display
{
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.core.Disposeable;
	
	import flash.display.Sprite;
	import flash.display.Stage3D;
	import flash.display3D.Context3D;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.utils.getTimer;

	public class LightStage extends LightContainer
	{
		public static var context3D:Context3D;
		public static var worldMatrix:Matrix3D = new Matrix3D();
		
		private var _stageWidth:int=0;
		private var _stageHeight:int=0;
		private var _frameRate:int=0;
		private var _color:int=0;
		private var _stage3D:Stage3D;


		public function LightStage(width:int, height:int, frameRate:int=30, color:int=0x000000)
		{
			_stageWidth=width;
			_stageHeight=height;
			_frameRate=frameRate;
			_color=color;
			init();
			initWorldMatrix();
		}

		/**
		 *
		 * @param width
		 * @param height
		 * @param frameRate
		 * @param color
		 *
		 */
		public function init():void
		{
			StageReferance.stage.frameRate=_frameRate;
			StageReferance.stage.addEventListener(Event.RESIZE, onResize);
			StageReferance.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			StageReferance.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			StageReferance.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_stage3D=StageReferance.stage.stage3Ds[0];
			_stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
			_stage3D.addEventListener(ErrorEvent.ERROR, onStage3DError);
			_stage3D.requestContext3D();
		}
		
		private function initWorldMatrix():void
		{
			var far:Number=1;
			var near:Number=-1;
			var coords:Vector.<Number>=new <Number>[2.0 / StageReferance.stage.stageWidth, 0.0, 0.0, 0.0,
													0.0, -2.0 /StageReferance.stage.stageHeight, 0.0, 0.0,
													0.0, 0.0, -2.0 / (far - near), 0.0, 
													-1.0, 1.0, -(far + near) / (far - near), 1.0];
			worldMatrix.copyRawDataFrom(coords);
		}

		public function sleep():void
		{

		}
		
		public function start():void
		{
			StageReferance.stage.addEventListener(Event.ENTER_FRAME,mainLoop);
		}
		
		public function pause():void
		{
			StageReferance.stage.removeEventListener(Event.ENTER_FRAME,mainLoop);
		}
		
		private function mainLoop(event:Event):void
		{
			// TODO Auto-generated method stub
			
			context3D.clear ( 1, 1, 1, 1 );
			render();
			context3D.present();
			
		}
		
		private function onStage3DError(event:ErrorEvent):void
		{
			// TODO Auto-generated method stub

		}

		private function onMouseMove(event:MouseEvent):void
		{
			// TODO Auto-generated method stub

		}

		private function onMouseUp(event:MouseEvent):void
		{
			// TODO Auto-generated method stub

		}

		private function onMouseDown(event:MouseEvent):void
		{
			// TODO Auto-generated method stub

		}

		private function onContextCreated(event:Event):void
		{
			// TODO Auto-generated method stub
			context3D = _stage3D.context3D;
			context3D.configureBackBuffer(StageReferance.stage.stageWidth, StageReferance.stage.stageHeight, 1, true);
		}

		private function onResize(event:Event):void
		{

		}

	}
}
